package com.qms.tubu.application;

import android.app.Application;
import android.content.SharedPreferences;


import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.bumptech.glide.integration.volley.VolleyUrlLoader;
import com.bumptech.glide.load.model.GlideUrl;
import com.qms.tubu.R;
import com.qms.tubu.adapter.BaseListAdapter;
import com.qms.tubu.bean.AccountBean;
import com.qms.tubu.tools.SPHelper;
import com.qms.tubu.tools.UrlConfig;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.smssdk.SMSSDK;
import tool.lib.OkHttpTool;
import tool.lib.ToastTool;
import tool.lib.volley.VolleyTool;


/**
 * 项目主要Application
 * Created by Mad on 2015/7/2.
 */
public class TubuApplication extends Application{
    private AccountBean mAccount;
    private List<OnAccountChangeListener> mListenerList;
    @Override
    public void onCreate() {
        super.onCreate();
        VolleyTool.init(this);
        UrlConfig.init(this);
        ToastTool.init(this);
        ShareSDK.initSDK(this);
        SMSSDK.initSDK(this, getResources().getString(R.string.smsdk_app_key), getResources().getString(R.string.smsdk_app_secret));
        Glide.get(this).register(GlideUrl.class, InputStream.class, new VolleyUrlLoader.Factory(VolleyTool.getInstance().getRequestQueue()));
        OkHttpTool.init();
        mListenerList=new ArrayList<OnAccountChangeListener>();
        mAccount=SPHelper.getAccount(getApplicationContext());
    }

    @Override
    public void onTerminate() {
        VolleyTool.release();
        super.onTerminate();
    }
    public void addChangeListener(OnAccountChangeListener listener)
    {
        mListenerList.add(listener);
    }
    public void removeChangeListener(OnAccountChangeListener listener)
    {
        mListenerList.remove(listener);
    }
    public AccountBean getAccount() {
        return mAccount;
    }

    public void setAccount(AccountBean account) {

        if(mAccount==null && account!=null){
            for(int i=0;i<mListenerList.size();i++)
            {
                mListenerList.get(i).onSignStateChanged(true);
            }
        }
        if(mAccount!=null && account==null)
        {
            for(int i=0;i<mListenerList.size();i++)
            {
                mListenerList.get(i).onSignStateChanged(false);
            }
        }
        mAccount=account;

    }
    public void notifyDataChanged()
    {
        for(int i=0;i<mListenerList.size();i++)
        {
            mListenerList.get(i).onDataChanged();
        }
    }
    public interface OnAccountChangeListener{
        void onDataChanged();
        void onSignStateChanged(boolean signState);// true为用户进行登录操作，false为用户进行登出操作
    }
}
